package com.the_ring.chapter11.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ScrollView;

public class PullDownScrollView extends ScrollView {

    public PullDownScrollView(Context context) {
        this(context, null);
    }

    public PullDownScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onScrollChanged(int l, int t, int oldl, int oldt) {
        super.onScrollChanged(l, t, oldl, oldt);
        boolean isScrolledToTop = false;
        boolean isScrolledToBottom = false;
        if (getScrollY() == 0) {
            isScrolledToTop = true;
            isScrolledToBottom = false;
        } else if (getScrollY() + getHeight() - getPaddingTop() - getPaddingBottom() == getChildAt(0).getHeight()) {
            isScrolledToTop = false;
            isScrolledToBottom = true;
        } else {
            isScrolledToTop = false;
            isScrolledToBottom = false;
        }
        if (mScrollListener != null) {
            if (isScrolledToTop) {
                mScrollListener.onScrolledToTop();
            } else if (isScrolledToBottom) {
                mScrollListener.onScrolledToBottom();
            }
        }
    }

    private ScrollListener mScrollListener;
    public void setScrollListener(ScrollListener mScrollListener) {
        this.mScrollListener = mScrollListener;
    }
    public interface ScrollListener {
        void onScrolledToBottom();
        void onScrolledToTop();
    }
}
